package com.doeis.jpush;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.doeis.minaserver.dao.User;
import com.doeis.push.PushBaiduDao;
import com.google.inject.Guice;
import com.google.inject.Injector;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.device.AliasDeviceListResult;
import cn.jpush.api.device.TagAliasResult;
import cn.jpush.api.push.model.Platform;

public class DevcieExample {
	protected static final Logger LOG = LoggerFactory.getLogger(DevcieExample.class);

	private static final String appKey = "f74b8f699a5c501a66a2b24b";
	private static final String masterSecret = "87505d95d27724cd7ee2471e";
	private static final String TAG1 = "tag1";
	private static final String ALIAS1 = "alias1";
	private static final String ALIAS2 = "alias2";
	private static final String REGISTRATION_ID1 = "0900e8d85ef";
	private static final String REGISTRATION_ID2 = "0a04ad7d8b4";

	private static JPushClient jpushClient = new JPushClient(masterSecret, appKey);

	public static void main(String[] args) {
		try {
//			System.err.println(Platform.android_ios().toJSON());
//			listUserByregistration();
			updateAlias();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void listUserByregistration() throws APIConnectionException, APIRequestException{
		Injector injector = Guice.createInjector();
		PushBaiduDao pushBaiduDao = injector.getInstance(PushBaiduDao.class);
		List< User> listUser = pushBaiduDao.query(User.class, "select * from user", false);
		AliasDeviceListResult listResult  = null;
		for(User user : listUser ){
			listResult  = jpushClient.getAliasDeviceList(user.getId(), "android");
			if(listResult.registration_ids==null){
				continue ;
			}
			if(listResult.registration_ids.size() ==0 ){
				continue ;
			}
			
			if(listResult.registration_ids.contains("0504712300c")){
				System.out.println( user.getId()+"  "+user.getName()+ " android " + listResult.registration_ids.toString());
			}
		}
	}
	
	public static void listUser() throws APIConnectionException, APIRequestException{
		Injector injector = Guice.createInjector();
		PushBaiduDao pushBaiduDao = injector.getInstance(PushBaiduDao.class);
		List< User> listUser = pushBaiduDao.query(User.class, "select * from user", false);
		AliasDeviceListResult listResult  = null;
		for(User user : listUser ){
			listResult  = jpushClient.getAliasDeviceList(user.getId(), "android");
			if(listResult.registration_ids==null){
				continue ;
			}
			if(listResult.registration_ids.size() ==0 ){
				continue ;
			}
			System.out.println( user.getId()+"  "+user.getName()+ " android " + listResult.registration_ids.toString());
			
			listResult  = jpushClient.getAliasDeviceList(user.getId(), "ios");
			if(listResult.registration_ids==null){
				continue ;
			}
			if(listResult.registration_ids.size() ==0 ){
				continue ;
			}
			System.err.println( user.getId()+"  "+user.getName()+ " ios " + listResult.registration_ids.toString());
		}
	}
	
	/**
	 * f5cfd1e652e7f9e90152e8c99d494400  罗美群 android [02064093fd8, 0504712300c]
	 * 方法介绍  清楚一个别名绑定多个手机的问题
	 * @throws APIConnectionException
	 * @throws APIRequestException
	 * @author tianbx
	 * @version 2016年5月23日下午4:27:44
	 */
	public static void updateAlias() throws APIConnectionException, APIRequestException{
		
		AliasDeviceListResult listResult  = jpushClient.getAliasDeviceList("f5cfd1e652e7f9e90152e8c99d494400", "android");
		System.out.println(listResult.toString());
		listResult  = jpushClient.getAliasDeviceList("f5cfd1e652e7f9e90152e8c99d494400", "ios");
		System.out.println(listResult.toString());
		TagAliasResult aliasResult = jpushClient.getDeviceTagAlias("02064093fd8");
		System.err.println(aliasResult.toString());
		
//		jpushClient.deleteAlias("f5cfd1e652e7f9e90152e8c99d494400", "android");
//		jpushClient.updateDeviceTagAlias("02064093fd8", "f5cfd1e652e7f9e90152e8c99d494400", null, null);
	}
	
	public static void testGetDeviceTagAlias() {
		try {
			TagAliasResult result = jpushClient.getDeviceTagAlias("0504712300c");
			LOG.info(result.alias);
			LOG.info(result.tags.toString());
			
			AliasDeviceListResult listResult  = jpushClient.getAliasDeviceList("ff8080814871feda014877427b310084", "android");
			System.out.println(listResult.toString());
			
//			jpushClient.deleteAlias("ff8080814871feda014877427b310084", "android");
//			
//			listResult  = jpushClient.getAliasDeviceList("ff8080814871feda014877427b310084", "android");
//			System.out.println(listResult.toString());
//			
//			
//			listResult  = jpushClient.getAliasDeviceList("ff8080814871feda014877427b310084", "ios");
//			System.out.println(listResult.toString());
//			
//			jpushClient.deleteAlias("ff8080814871feda014877427b310084", "ios");
//			
//			listResult  = jpushClient.getAliasDeviceList("ff8080814871feda014877427b310084", "ios");
//			System.out.println(listResult.toString());
			
		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);
			
		} catch (APIRequestException e) {
			LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
		}
	}
	
}


